package com.auth.controller;

import com.auth.common.result.Result;
import com.auth.custom.LoginUserInfoHelper;
import com.auth.model.system.SysUser;
import com.auth.service.SysMenuService;
import com.auth.service.SysUserService;
import com.auth.utils.BooleanUtils;
import com.auth.vo.system.LoginVo;
import com.auth.vo.system.RouterVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import exception.BusinessException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import jwt.JwtHelperUtils;
import org.springframework.web.bind.annotation.*;
import utils.Md5Utils;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;

/**
 * @Author:Tangjiachang
 * @Date 2023/7/13
 * @Description:
 */
@Api(tags = "后台登录接口")
@RestController
@RequestMapping("/admin/system/index")
public class IndexController {
    private final static String AVATER = "https://oss.aliyuncs.com/aliyun_id_photo_bucket/default_handsome.jpg";
    private final SysUserService sysUserService;
    private final SysMenuService sysMenuService;
    public IndexController(SysUserService sysUserService,
                           SysMenuService sysMenuService) {
        this.sysUserService = sysUserService;
        this.sysMenuService = sysMenuService;
    }

    @ApiOperation("登录接口")
    @PostMapping("/login")
    public Result login(@RequestBody LoginVo loginVo) {
        SysUser user = sysUserService.getOne(new LambdaQueryWrapper<SysUser>()
                .eq(SysUser::getUsername, loginVo.getUsername()));
        if (null == user) {
            throw new BusinessException(201, "用户不存在");
        }
        // 判断用户是否被禁用
        if (user.getStatus() == 0) {
            throw new BusinessException(201, "用户被禁用");
        }
        // 判断密码
        if (BooleanUtils.not(Md5Utils.encrypt(loginVo.getPassword())
                .equals(user.getPassword()))) {
            throw new BusinessException(201, "密码错误");
        }

        return Result.ok(new HashMap<String, Object>(0) {{
            put("token", JwtHelperUtils.createToken(user.getId(), user.getName()));
        }});
    }

    @ApiOperation("获取用户信息接口")
    @GetMapping("/info")
    public Result info(HttpServletRequest request) {
        // 从请求头获取参数
        String token = request.getHeader("token");
        // 获取用户id以及用户名称
        Long userId = JwtHelperUtils.getUserId(token);
        SysUser user = sysUserService.getById(userId);
        // 根据用户id获取用户可以操作的菜单列表，动态构建前端路由
        List<RouterVo> routerVos = sysMenuService.findUserMenuListByUserId(userId);
        // 根据用户id获取用户可以操作的按钮列表
        List<String> perms =  sysMenuService.findUserPermsByUserId(userId);
        return Result.ok(new HashMap<String, Object>(0) {{
            put("roles",user.getRoleList());
            put("name",user.getName());
            put("avatar",AVATER);
            put("routers", routerVos);
            put("buttons", perms);
        }});
    }

    @ApiOperation("退出登录接口")
    @PostMapping("/logout")
    public Result logout() {
        LoginUserInfoHelper.removeAll();
        return Result.ok();
    }

}
